﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace 花店管理系统.Tproduct
{
    public partial class Update_product : Form
    {
        public Update_product()
        {
            InitializeComponent();
        }

        SqlDataAdapter daProduct, daLog;
        DataSet ds = new DataSet();
        public static string path_source = "";//保存图片路径
        void init()
        {
            DB.GetCn();
            string str = "select * from Tgood";
            string sdr = "select * from log";
            daProduct = new SqlDataAdapter(str, DB.cn);
            daLog = new SqlDataAdapter(sdr, DB.cn);
            daProduct.Fill(ds, "product_info");
            daLog.Fill(ds, "log_info");
            DB.cn.Close();
        }
        void showAll()
        {
            DataView dvProduct = new DataView(ds.Tables["product_info"]);
            dgvProduct.DataSource = dvProduct;
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void Update_product_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“huadianDataSet4.Tgongyingshang”中。您可以根据需要移动或移除它。
            this.tgongyingshangTableAdapter.Fill(this.huadianDataSet4.Tgongyingshang);
            // TODO: 这行代码将数据加载到表“huadianDataSet3.Tleibie”中。您可以根据需要移动或移除它。
            this.tleibieTableAdapter.Fill(this.huadianDataSet3.Tleibie);
            init();
            showAll();
        }

        private void dgvProduct_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtId.Text = dgvProduct.CurrentRow.Cells["G_id"].Value.ToString();
            cboCategoryId.Text = dgvProduct.CurrentRow.Cells["L_id"].Value.ToString();
            txtPrice.Text = dgvProduct.CurrentRow.Cells["G_xiaoshoujia"].Value.ToString();
            txtUnit.Text = dgvProduct.CurrentRow.Cells["G_chenbenjia"].Value.ToString();
            cboSupp.Text = dgvProduct.CurrentRow.Cells["Y_id"].Value.ToString();
            txtName.Text = dgvProduct.CurrentRow.Cells["G_name"].Value.ToString();
            txtDescn.Text = dgvProduct.CurrentRow.Cells["G_miaosu"].Value.ToString();
            txtQty.Text = dgvProduct.CurrentRow.Cells["G_kuchun"].Value.ToString();
            try
            {
                pic_Product.Image = Image.FromFile(Application.StartupPath
                    + dgvProduct.CurrentRow.Cells["Image"].Value.ToString());
                pic_Product.SizeMode = PictureBoxSizeMode.StretchImage;
            }
            catch
            {
                pic_Product.Image = Image.FromFile(Application.StartupPath + "\\" + "暂无图片.png");
            }
        }

        private void btnPic_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if(ofd.ShowDialog()==DialogResult.OK)
            {
                path_source = ofd.FileName;
                pic_Product.Image = Image.FromFile(path_source);
                pic_Product.SizeMode = PictureBoxSizeMode.StretchImage;

            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            ValidateUpdateInput();
        }

        private void ValidateUpdateInput()
        {
            if (txtName.Text == "" || txtUnit.Text == "" || txtQty.Text == "")
            {
                MessageBox.Show("商品名称、成本价格、库存不能为空");

            }
            else
            {
                DialogResult dr = MessageBox.Show("您确定要修改吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.OK)
                {
                    string filename;
                    string fileFolder;
                    string dateTime = "";
                    filename = Path.GetFileName(path_source);
                    dateTime += DateTime.Now.Year.ToString();
                    dateTime += DateTime.Now.Month.ToString();
                    dateTime += DateTime.Now.Day.ToString();
                    dateTime += DateTime.Now.Hour.ToString();
                    dateTime += DateTime.Now.Minute.ToString();
                    dateTime += DateTime.Now.Second.ToString();
                    filename = dateTime + filename;
                    fileFolder = Directory.GetCurrentDirectory() + "\\" + "Prod_Images\\"
                        + cboCategoryId.SelectedText + "\\";
                    fileFolder += filename;
                    DataRow[] ProRows = UpdateProductData();
                    if (path_source != "")
                    {
                        File.Copy(path_source, fileFolder, true);
                        ProRows[0]["Image"] = "\\Prod_Images\\" + cboCategoryId.SelectedText + "\\"
                            + filename;
                    }
                    DataRow drLog = ds.Tables["log_info"].NewRow();
                    drLog["tname"] = Login.username;
                    drLog["L_type"] = "修改";
                    drLog["L_date"] = DateTime.Now;
                    drLog["L_table"] = "Tgood表";
                    ds.Tables["log_info"].Rows.Add(drLog);
                    try
                    {
                        SqlCommandBuilder dbProduct = new SqlCommandBuilder(daProduct);
                        daProduct.Update(ds, "product_info");
                        SqlCommandBuilder dbLog = new SqlCommandBuilder(daLog);
                        daLog.Update(ds, "log_info");
                        MessageBox.Show("修改成功");
                        path_source = "";
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    DB.cn.Close();
                }
            }
        }

        private DataRow[] UpdateProductData()
        {
            int a = currentRowIndex();
            string str = productId(a);
            DataRow[] ProRows = ds.Tables["product_info"].Select("G_id='" + str + "'");
            ProRows[0]["L_id"] = cboCategoryId.SelectedValue;
            ProRows[0]["G_xiaoshoujia"] = decimal.Parse(txtPrice.Text.Trim());
            ProRows[0]["G_chenbenjia"] = decimal.Parse(txtUnit.Text.Trim());
            ProRows[0]["Y_id"] = cboSupp.SelectedValue;
            ProRows[0]["G_name"] = txtName.Text.Trim();
            ProRows[0]["G_miaosu"] = txtDescn.Text.Trim();
            ProRows[0]["G_kuchun"] = int.Parse(txtQty.Text.Trim());
            return ProRows;
        }

        private string productId(int a)
        {
            return dgvProduct.Rows[a].Cells["G_id"].Value.ToString();
        }

        private int currentRowIndex()
        {
            return dgvProduct.CurrentRow.Index;
        }

        private void btnCance_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label8_Click(object sender, EventArgs e)
        {

        }
    }
}
